<?php

namespace App\Http\Controllers\Refund;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Refund\Refund;
use App\Models\Balance\Balance;
use App\Models\Custom\Custom;
use App\Models\Refund\Refund as ModelRefund;
use App\Services\Common\SnCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 退款管理
 */
class RefundController extends BaseController
{

    /**
     * 添加
     */
    public function dopostadd(Request $request)
    {
        $params = ['custom_id', 'custom_name', 'refund_amount'];
        $req_data = $request->only($params);
        $validate = new Refund('add');
        if (!$validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $validate->getError()]);
        }

        $mangerinfo = $this->mangerinfo;
        $manger_id = $mangerinfo['id'] ?? 0;
        $cpid = $mangerinfo['cpid'] ?? 0;

        $custom_id = $req_data['custom_id'];
        $refund_amount = $req_data['refund_amount'];

        $custom = Custom::where([
            'cpid' => $cpid,
            'id' => $custom_id,
            'manger_id' => $manger_id
        ])->first();
        if (!$custom) {
            return response()->json(['code' => '001', 'msg' => '客户不存在']);
        }
        $custom = $custom->toArray();

        //余额是否充足
        $where = [
            'cpid' => $cpid,
            'custom_id' => $custom_id,
            'manger_id' => $manger_id
        ];
        $balance = Balance::where($where)->first();
        if (!$balance) {
            return response()->json(['code' => '001', 'msg' => '客户不存在']);
        }
        $balance = $balance->balance;
        if ($balance < $refund_amount) {
            return response()->json(['code' => '001', 'msg' => '余额不足']);
        }

        $data = [
            'cpid' => $cpid,
            'manger_id' => $manger_id,
            'manger_name' => $custom['company_name'],
            'date' => time(),
            'code' => SnCode::getSnCode('TK_'),
            'settlement_company' => $custom['settlement_company'],
            'custom_id' => $custom_id,
            'custom_name' => $req_data['custom_name'],
            'balance' => $refund_amount,
            'status' => config('constants.order_status.audit'),
            'create_at' => time()
        ];

        DB::beginTransaction();
        try {
            //扣款
            Balance::where($where)
                ->update([
                    'balance' => DB::raw('balance - ' . $refund_amount),
                    'update_at' => time()
                ]);
            //发起退款
            ModelRefund::create($data);
            //todo 钉钉提审
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return response()->json(['code' => '001', 'msg' => '退款添加失败' . $e->getMessage()]);
        }
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 审核退款，通过、驳回
     */
    public function handlerefund()
    {
        //通过

        //驳回，加余额
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $memberinfo = $this->mangerinfo;
        $isgetall = $request->isgetall ?? 0;
        $list = ModelRefund::getList($request, $memberinfo, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }
}
